<?php
/**
 * Controller pre hosting
 * @author Tomas Srna
 * @package bakalarka
 */

class HostingSettingsController extends HostingAbstractController
{
	/**
	 * V tomto controlleri musime mat vybraty hosting
	 * a ked mame id, musi byt nas zaznam
	 */
	public function preDispatch()
	{
		parent::preDispatch();
		
		if(!property_exists($this, 'hosting'))
		{
			throw new Exception('L::errorObjectNotFound;'.
				json_encode(array('HostingModel(?)')));
		}
		
		
		// Pri settings nemame id
		/*if(array_key_exists('id', $_GET))
		{
			$hosting_id = Selector::select('id')
				->from(HostingModel::struct())
				->where('name = ?', $this->hosting)
				->fetch()->current()->id;
			$test = DnsRecordModel::load(array('id' => $_GET['id']));
			if($test && $test->domain_id != $domain_id)
				throw new PermissionDeniedException('L::badContext;'.
					json_encode(array('DnsRecordModel('.$_GET['id'].')')
				), 'DnsRecordModel('.$_GET['id'].')');
		}*/
	}
	
	/**
	 * Index
	 */
	public function index()
	{
		$t = Template::instance();
		
		$t->plusmenu = $this->plusmenu;
		$t->plusmenu_title = $this->plusmenu_title;
		
		$t->addHook('headEnd', "<link rel=\"stylesheet\" ".
			"type=\"text/css\" href=\"".APPBASE."css/form.css\" />");
		
		$hosting = Selector::select()
			->from(HostingModel::struct())
			->where('name = ?', $this->hosting)
			->fetch()->current();
		
		$t->title = L::selected()->settings;
		
		$t->text = '<p class="hint">'.L::selected()->hostingRefresh.'</p>';
		
		$ed = new EnabledDisabledWriter();
		
		$t->properties = array(
			L::selected()->documentroot => $hosting->documentroot,
			L::selected()->adminmail => $hosting->adminmail,
			L::selected()->php => $ed->set($hosting->php),
			L::selected()->ssl => $ed->set($hosting->ssl),
			L::selected()->subdomaindir=> $hosting->subdomaindir,
			L::selected()->symlinks => $ed->set($hosting->symlinks),
			L::selected()->indexes => $ed->set($hosting->indexes),
			L::selected()->allowoverride => $ed->set($hosting->allowoverride),

		);
		
		$t->buttons = array(
			new Button('pencil.png', L::selected()->edit, 
				sprintf("window.location.href = '%s%s/%s/%s?redir=%s';", 
					APPBASE, 'hosting', $this->hosting, 'settings/edit',
					APPBASE.'hosting/'.$this->hosting.'/settings')),
		);
		
		$t->render('property-list');
	}

	/**
	 * Edit
	 */
	public function edit()
	{
		$t = Template::instance();
		
		$t->hosting = $this->hosting;
		
		$t->plusmenu = $this->plusmenu;
		$t->plusmenu_title =$this->plusmenu_title;
		
		$t->title = L::selected()->editHostingSettings;
		
		$model = Selector::select()
			->from(HostingModel::struct())
			->where('name = ?', $this->hosting)
			->fetch()->current();
		
		if(getenv('REQUEST_METHOD') == 'POST')
		{
			$f = new HostingSettingsForm(&$t, $_POST);
			
			if($f->validate())
			{
					$model->set($_POST);
					
					// Pridame dalsie povinne veci
					$model->updated = new Expression('NOW()');
					
					$model->save();
					
					//die('<pre>'.print_r($model->sql(), true).'</pre>');
					
					Logger::log('L::objectChanged_s;'.
						json_encode((string)$model), 'info');
						
					$this->autoredir();
			}
			else
				$t->error = L::selected()->invalidForm;
		}
		else
		{
			$f = new HostingSettingsForm(&$t, $model);
		}
		
		$fw = new FormWriter($f);
		$t->fw = $fw;
		
		$t->render('form');
	}
}
